<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no"/>
		<title>故障定位</title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
		<link rel="stylesheet" type="text/css" href="../../css/common.css" />
		<link rel="stylesheet" type="text/css" href="../bill/css/supportDefineDetail.css" />
	</head>
	<body>
		<div id="wrap">
			<div id="sddDivId" style="overflow:auto;margin-bottom:55px;">
				<div  id="transferLabelDivId" class="selectLabelDiv" style="margin-top: 10px;display: none" >
					<div id="user_name" style="font-size:16px;color:#adadad;">
						接收人
					</div>
				</div>
				<div id="telId" class="cardDiv" style="display: none;">
					<div class="cardLabel">
						联系电话：
					</div>
					<div class="cardCon">
						<input id="contactTelTextId" type="text"  style="background-color:#FFFFFF;width:100%;height:100%;line-height:40px;">
					</div>
				</div>
				<div id="locaId" class="cardTextareaDiv" style="display: none;">
					<div class="cardTextareaLabel">
						所在位置：
					</div>
					<div class="cardTextareaCon">
						<textarea id="locationId" rows="10" readonly="readonly" style="overflow:auto;background-color: #FAFAFA "></textarea>
					</div>
				</div>
				<div class="cardTextareaDiv" id="err_cause">
					<div class="cardTextareaLabel">
						偏离说明：
					</div>
					<div class="cardTextareaCon">
						<input type="hidden" id="distance"/><!--偏差-->
						<input type="hidden" id="describe"/><!--描述-->
						<textarea id="otherDealInfoId" rows="4"  style="overflow:auto; "></textarea>
					</div>
				</div>
			</div>
			<footer id="footId" style="height:50px;display: none;">
				<div class="cardButtonDiv">
					<button class="cancelBtn"  onclick="signOptFunc();">
						确定
					</button>
					<button class="okBtn"  onclick="restartLocationFunc();">
						重新定位
					</button>
				</div>
			</footer>
		</div>
	</body>
	<script src="../../script/jquery-1.11.3.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/doT.min.js"></script>
	<script type="text/javascript" src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/config.js"></script>
	<script type="text/javascript" src="../../script/client.api.js"></script>
	<script type="text/javascript" src="../../script/aui-alert.js"></script>
	<script type="text/javascript" src="../../script/debug.js"></script>
	<script type="text/javascript" src="../../script/request.js"></script>
	<script type="text/javascript">
		var idclean = "";
		var type = "";
		var isSuper = "N";
    	var firstDate = new Date();
		request.billStationSign = function (opts,callback) {
			service({
				url: "/enterStationBill/fixBillInspection.do",
				data: opts
			},callback)
		}
		apiready = function() {
			api.addEventListener({
				name : 'viewappear'
			}, function(ret, err) {
				api.closeFrame({
					name : 'standBillMenu'
				});
			});
			restartLocationFunc();
			api.setStatusBarStyle({
				style : 'light'
			});
		};
		//初始化故障定位信息
		function initRelief() {
			console.log("==================initRelief===================")
			var user = $api.getStorage('user');
			var billModel = $api.getStorage('standBillModel');
			
			$api.byId('transferLabelDivId').style.display = "";
			$api.byId('telId').style.display = "";
			$api.byId('locaId').style.display = "";
			$api.byId('footId').style.display = "";
			$api.byId('user_name').innerHTML = "操作人：" + user.username;
			$api.byId('contactTelTextId').value = user.mobilephone;
			type = api.pageParam.operateType;
			
			var location = $api.getStorage('LAST_GPS_LOCATION1');
			var lon1 = $api.getStorage("lon1");
			var lat1 = $api.getStorage("lat1");
			lon = location.lon != null ? location.lon : lon1;
			lat = location.lat != null ? location.lat : lat1;
			var stLon = billModel.st_lon;
			var stLat = billModel.st_lat;
			if(stLon==null) stLon="0";
			if(stLat==null) stLat="0";
			lon = displaynum(lon);
			console.log("测试",stLat,stLon,lat,lon);
			lat = displaynum(lat);
			var info = "";
			var distance = GetDistance(stLat, stLon, lat, lon);
			
			console.log(distance)
			var describe="";
			if (distance <= 100) {
				describe="偏差在100米范围内，可以进行手机定位";
				info = "站址经度是：" + stLon + "\n站址纬度是：" + stLat + "\n当前经度是：" + lon + "\n当前纬度是：" + lat + "\n当前时间：" + location.time + "\n偏差：" + distance + "米" + "\n情况描述："+describe;
				isSuper = "N";
			} else if (distance > 100) {
				describe="偏差已经超过100米，请在空旷的地方进行重新定位或者填写偏离原因";
				console.log("时间：" + location.timestamp);
				info = "站址经度是：" + stLon + "\n站址纬度是：" + stLat + "\n当前经度是：" + lon + "\n当前纬度是：" + lat + "\n当前时间：" + location.time + "\n偏    差：" + distance + "米" + "\n情况描述："+describe;
				isSuper = "Y";
				
			}
			$api.byId('locationId').innerHTML = info;
			$api.byId('distance').value=distance
			$api.byId('describe').value=describe
		}
		function signOptFunc() {
			billModel = $api.getStorage('standBillModel');
			var user = $api.getStorage('user');
			var userId = user.userid;
			var userName = user.username;
			var id= billModel.id;
			var billSn = billModel.billsn;
			var taskId = billModel.taskid;
			var linkInfo = $api.byId('contactTelTextId').value;
			var remark = $api.byId('locationId').value;
			var causeDealInfo = $api.byId('otherDealInfoId').value;
			var distance = $api.byId('distance').value;
			var describe = $api.byId('describe').value;
			var time = new Date().pattern("yyyy-MM-dd HH:mm:ss");
			var otherValue = $api.byId('otherDealInfoId').value;
			if(distance>1000 && otherValue==''){
				api.toast({
					msg : '位置已超出100米，请填写偏差说明'
				});
				return;
			}
			api.showProgress({
				title : '处理中',
				modal : true
			});
			console.log("type:" + type);
			var opts={
				id : id,
				userId:userId,
				userName:userName,
				signdate : time,
				signlon : lon,
				signlat : lat,
				distance : distance,
				describe : describe,
				causeDealInfo : causeDealInfo			
			};
			
			// 计算操作时长
			var endDate=new Date(); 
			opts.operatingCost = (endDate.getTime()-firstDate.getTime())/1000; 
			request.billStationSign(opts,function(ret, err) {
				console.log("============签到================")
				console.log(JSON.stringify(ret));
				if (ret) {
					if (ret.success) {
						api.toast({
							msg : "签到成功，请进行身份识别",
							duration : 3000,
							location : 'bottom'
						});
						
						//开始面部识别
						window.setTimeout(function() {
							//api.closeWin();
							api.openWin({
								name : "billStationFaceCheck_win",
								url : "billStationFaceCheck_win.html",
								bounces : false,
								rect : {
									x : 0,
									y : 0,
									w : 'auto',
									h : 'auto'
								},
								reload : true,
								pageParam : api.pageParam
							});
						}, 1100);
					} else {
						api.toast({
							msg : ret.msg,
							location : 'bottom'
						});
					}
				} else {
					api.toast({
						msg : '接口访问失败',
						location : 'bottom'
					});
				}
				api.hideProgress();
			});
		}

		function restartLocationFunc() {
			//easyDialog.close({
			//		container : "SignDiv"
			//	});
			api.showProgress({
				title : '定位中……',
				modal : true
			});
			getHeartFromGps("billStationSign_win", "billStationSign_frm");
		}

		function getHeartFromGps(winName, frmName) {
			var script = "startLocation('" + winName + "','" + frmName + "');";
			console.log("异步加载" + script);
			api.execScript({
				name : "home_win",
				frameName : 'gps',
				script : script
			});
			//      gps_invoke();
		}

		function gps_invoke() {
			var lastLocation = $api.getStorage('LAST_GPS_LOCATION1');
			console.log("经度1：" + lastLocation.lon);
			console.log("定位时间：" + lastLocation.timestamp);
			if (!lastLocation || !lastLocation.status || lastLocation.timestamp == 0 || lastLocation.lon == 0 || lastLocation.lat == 0) {
				return;
			}
			$api.setStorage("lon1", lastLocation.lon);
			$api.setStorage("lat1", lastLocation.lat);
			api.showProgress({
				title : '定位中……',
				modal : true
			});
			window.setTimeout(function() {
				api.hideProgress();
				api.execScript({
					frameName : "billStationSign_frm",
					script : 'initRelief();'
				});
				//							api.openWin({
				//					        name: 'upStationSign_win',
				//					        url: 'upStationSign_win.html',
				//					        bounces: false,
				//					        rect: {
				//					            x: 0,
				//					            y: 0,
				//					            w: 'auto',
				//					            h: 'auto'
				//					        },
				//					        reload: true,
				//					        pageParam:api.pageParam
				//					    });
			}, 2000);
		}

		function closeWin() {
			api.closeWin({
				name : 'supportDefine_win'
			});
		}
	</script>
</html>